package com.micro.ai.template.controller;

import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.template.entity.TemplateStatistic;
import com.micro.ai.template.service.TemplateStatisticService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 模板统计分析Controller
 *
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/template/statistics")
@RequiredArgsConstructor
@Tag(name = "模板统计分析", description = "模板统计分析相关接口")
public class TemplateStatisticController {

    private final TemplateStatisticService templateStatisticService;

    @GetMapping("/{templateId}")
    @PreAuthorize("hasAuthority('template:stat:query')")
    @Operation(summary = "查询模板统计", description = "获取模板的统计信息")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "未授权"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "无权限"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "模板不存在")
    })
    public ApiResponse<Map<String, Object>> getStatistics(
            @Parameter(description = "模板ID") @PathVariable String templateId) {
        log.info("查询模板统计, templateId: {}", templateId);
        return ApiResponse.success(templateStatisticService.getStatistics(templateId));
    }

    @GetMapping("/{templateId}/detail")
    @PreAuthorize("hasAuthority('template:stat:query')")
    @Operation(summary = "查询统计详情", description = "获取模板统计的详细记录")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功",
            content = @Content(schema = @Schema(implementation = TemplateStatistic.class))),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "未授权"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "无权限")
    })
    public ApiResponse<TemplateStatistic> getStatisticDetail(
            @Parameter(description = "模板ID") @PathVariable String templateId) {
        log.info("查询统计详情, templateId: {}", templateId);
        return ApiResponse.success(templateStatisticService.getOrCreateStatistic(templateId));
    }

    @PostMapping("/{templateId}/usage")
    @PreAuthorize("hasAuthority('template:stat:update')")
    @Operation(summary = "增加使用次数", description = "记录模板使用")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "更新成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "未授权"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "无权限"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "模板不存在")
    })
    public ApiResponse<Void> incrementUsage(
            @Parameter(description = "模板ID") @PathVariable String templateId) {
        log.info("增加使用次数, templateId: {}", templateId);
        templateStatisticService.incrementUsageCount(templateId);
        return ApiResponse.<Void>success(null);
    }

    @PostMapping("/{templateId}/success")
    @PreAuthorize("hasAuthority('template:stat:update')")
    @Operation(summary = "增加成功次数", description = "记录模板成功执行")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "更新成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "未授权"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "无权限"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "模板不存在")
    })
    public ApiResponse<Void> incrementSuccess(
            @Parameter(description = "模板ID") @PathVariable String templateId) {
        log.info("增加成功次数, templateId: {}", templateId);
        templateStatisticService.incrementSuccessCount(templateId);
        return ApiResponse.<Void>success(null);
    }

    @PostMapping("/{templateId}/failure")
    @PreAuthorize("hasAuthority('template:stat:update')")
    @Operation(summary = "增加失败次数", description = "记录模板执行失败")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "更新成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "未授权"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "无权限"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "模板不存在")
    })
    public ApiResponse<Void> incrementFailure(
            @Parameter(description = "模板ID") @PathVariable String templateId) {
        log.info("增加失败次数, templateId: {}", templateId);
        templateStatisticService.incrementFailureCount(templateId);
        return ApiResponse.<Void>success(null);
    }

    @PostMapping("/{templateId}/rating")
    @PreAuthorize("hasAuthority('template:stat:update')")
    @Operation(summary = "更新平均评分", description = "更新模板的平均评分")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "更新成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "参数错误"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "未授权"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "无权限"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "模板不存在")
    })
    public ApiResponse<Void> updateRating(
            @Parameter(description = "模板ID") @PathVariable String templateId,
            @Parameter(description = "评分") @RequestParam double rating) {
        log.info("更新平均评分, templateId: {}, rating: {}", templateId, rating);
        templateStatisticService.updateAverageRating(templateId, rating);
        return ApiResponse.<Void>success(null);
    }
}

